<?php
namespace app\models;

use yii\db\ActiveRecord;
use Yii;

class content extends ActiveRecord{
	public static function tableName(){
		return "{{%content}}";
	}

	public function rules(){
		return[
		    ['Content', 'required', 'message' => '提交的内容不能为空~'],
		    ['Content', 'string', 'max' => 600],
		];
	}

	public function add($data){
		if($data){
			//处理性别数据
			$checksex = $data['formSex'];
			if($checksex == "man"){
				$sex = 2;
			}else{
				$sex = 1;
			}
		 	//随机头像
			$Url_B = array("0" => "http://olohq71tr.bkt.clouddn.com/%E7%9D%A1%E4%B8%8D%E7%9D%80/URL5.jpg",
		                   "1" => "http://olohq71tr.bkt.clouddn.com/%E7%9D%A1%E4%B8%8D%E7%9D%80/URL6.jpg");
	        $Url_G = array("0" => "http://olohq71tr.bkt.clouddn.com/%E7%9D%A1%E4%B8%8D%E7%9D%80/URL7.jpg",
		                   "1" => "http://olohq71tr.bkt.clouddn.com/%E7%9D%A1%E4%B8%8D%E7%9D%80/URL8.jpg");
            $Url_r = rand(0,1);
	        if($sex == 2) {
	        	$url = $Url_B[$Url_r];
	        }else{
	        	$url = $Url_G[$Url_r];
	        }
	        //创建时间
	        date_default_timezone_set('PRC'); 
	        $Createtime= date('Y-m-d H:i:s');
	        //输入数据库
			$this->Content = $data['formContent'];
			$this->NickName = $data['formName'];
			$this->Sex = $sex;
			$this->Creattime = $Createtime;
			$this->Url = $url;
            $this->LikeCount = 0;
			$result = $this->save();
			if($result){
				$error = 0;
				return $error;
			}else{
				$error = $this->getErrors();
				return $error;
			}
		}else{
			$error = 2;
			return $error;
		}
	}
    
    public function mod($id,$include){
    	//评论数增加
    	$condition = [
            'Content_ID' => $id,
    	];
    	$keys = array_keys($include);
    	if($keys[0] == 'Comment'){
    		$data = [
    		    'Count_P' => $include['Comment']
    		];
    	}else if($keys[0] == 'LikeCount'){
    		$data = [
    		    'LikeCount' => $include['LikeCount']
    		];
    	}
    	$result = self::updateAll($data, $condition);
    	return $result;
    }


}